package com.jstyle.jclife.activity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jstyle.blesdk.cmdenum.SendCmdState;
import com.jstyle.blesdk.constant.DeviceConst;
import com.jstyle.blesdk.constant.DeviceKey;
import com.jstyle.jclife.NetWork.NetWorkUtil;
import com.jstyle.jclife.R;
import com.jstyle.jclife.adapter.MyWheelAdapter;
import com.jstyle.jclife.daoManager.UserInfoDaoManager;
import com.jstyle.jclife.model.UserInfo;
import com.jstyle.jclife.utils.SharedPreferenceUtils;
import com.jstyle.jclife.view.BpCalibrationDialog;
import com.jstyle.jclife.view.widget.views.WheelView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BPCalibrationActivity extends BaseActivity implements BpCalibrationDialog.CalibrationResultListener {

    @BindView(R.id.button)
    Button button;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.view)
    View view;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.wheelView_highsp)
    WheelView wheelViewHighsp;
    @BindView(R.id.wheelView_lowsp)
    WheelView wheelViewLowsp;
    private MyWheelAdapter wheelAdapterHighSp;
    private MyWheelAdapter wheelAdapterLowSp;
    List<String> highSpList;
    List<String> lowSpList;
    private BpCalibrationDialog bpCalibrationDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bpcalibration);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        highSpList = getListData(0, 250);
        lowSpList = getListData(0, 250);
        int highBPCalibration = SharedPreferenceUtils.getInteger(SharedPreferenceUtils.KEY_BPCalibration_High, 120);
        int lowBPCalibration = SharedPreferenceUtils.getInteger(SharedPreferenceUtils.KEY_BPCalibration_Low, 80);
        int currentHigh = highSpList.indexOf(highBPCalibration + "");
        int currentLow = lowSpList.indexOf(lowBPCalibration + "");
        wheelAdapterHighSp = new MyWheelAdapter(this, highSpList, currentHigh, 20, 18);
        wheelViewHighsp.setViewAdapter(wheelAdapterHighSp);
        wheelViewHighsp.setLabel("SP", Gravity.RIGHT);
        wheelViewHighsp.setVisibleItems(7);
        wheelAdapterLowSp = new MyWheelAdapter(this, lowSpList, currentLow, 20, 18);
        wheelViewLowsp.setViewAdapter(wheelAdapterLowSp);
        wheelViewLowsp.setLabel("DP", Gravity.RIGHT);
        wheelViewLowsp.setVisibleItems(7);

    }

    private List<String> getListData(int start, int end) {
        List<String> list = new ArrayList<>();
        for (int i = start; i < end; i++) {
            list.add(String.format("%02d", i));
        }
        return list;
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    private void updateValue() {
        int highValue = Integer.valueOf(wheelAdapterHighSp.getCurrentItemText());
        int lowValue = Integer.valueOf(wheelAdapterLowSp.getCurrentItemText());
        String account = NetWorkUtil.getUserId();
        UserInfo userInfo = UserInfoDaoManager.getUserByUid(account);
        if (userInfo == null) {
            userInfo = new UserInfo();
            userInfo.setUserId(account);
            UserInfoDaoManager.insertUser(userInfo);
        }
        String birthday = userInfo.getBirthday();
        int userYear = Integer.parseInt(birthday.split("-")[0]);
        Calendar calendar = Calendar.getInstance();
        int age = calendar.get(Calendar.YEAR) - userYear;
        int gender = userInfo.getGender();
        byte[] value = new byte[16];
        value[0] = DeviceConst.CMD_Enable_ECGPPG;
        value[2] = 1;
        value[3] = (byte) (gender == 0 ? 1 : 0);
        value[4] = (byte) age;
        value[5] = (byte) userInfo.getHeight();
        value[6] = (byte) userInfo.getWeight();
        value[7] = (byte) highValue;
        value[8] = (byte) lowValue;
        value[9] = 70;
        writeData(value);
    }

    @OnClick({R.id.iv_back, R.id.bt_confim})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_confim:
                SharedPreferenceUtils.setSpInteger(SharedPreferenceUtils.KEY_BPCalibration_High, Integer.valueOf(wheelAdapterHighSp.getCurrentItemText()));
                SharedPreferenceUtils.setSpInteger(SharedPreferenceUtils.KEY_BPCalibration_Low, Integer.valueOf(wheelAdapterLowSp.getCurrentItemText()));
                updateValue();
                showBpCalibrationDialog();
                break;
        }
    }

    private void showBpCalibrationDialog() {
        bpCalibrationDialog=new BpCalibrationDialog(this, R.style.jstyleDialog_style);
        bpCalibrationDialog.setCalibrationListener(this);
        bpCalibrationDialog.show();
    }

    @Override
    public void dataCallback(Map<String, String> maps, SendCmdState sendCmdState) {
        super.dataCallback(maps, sendCmdState);
        switch (sendCmdState) {
            case ResultEcg:
                Integer result = Integer.valueOf(maps.get(DeviceKey.ECGPPGResult));
                if (result ==13|| result == 14 ||result==2) {
                    bpCalibrationDialog.setBpCalibrationFinish(result);
                }

                break;
        }
    }

    @Override
    public void onCalibrationSuccess() {
            finish();
    }

    @Override
    public void onCalibrationFailed() {

    }
}
